package com.qf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;

/**
 * 上传
 * @author 千锋健哥
 */
@Controller
@RequestMapping("/upload")
public class UploadController {

    /**
     * 跳转到上传页面
     * @return
     */
    @RequestMapping("/toUpload")
    public String toUpload() {
        return "fileupload";
    }

    /**
     * 处理上传文件请求
     * @return
     */
    @RequestMapping("/test1")
    @ResponseBody
    public String test1(MultipartFile source, HttpSession session) throws Exception{
        //文件名称
        String fileName = source.getOriginalFilename();
        //文件大小
        long size = source.getSize();
        //文件路径 + 文件名
        String realPath = session.getServletContext().getRealPath("/upload");
        String path = realPath + "\\" + fileName;
        //保存文件到硬盘中
        source.transferTo(new File(path));
        //TODO 将文件保存的路径和文件名存入数据库对应的表的字段中
        return path;
    }
}
